/*
 * @Author: Wangjun
 * @Date: 2023-02-27 14:48:23
 * @LastEditTime: 2025-06-13 17:40:13
 * @LastEditors: wangjun haodreams@163.com
 * @Description:
 * @FilePath: \xrop2opd:\go\src\gitee.com\haodreams\libs\fixedqueue\queue.go
 * hnxr
 */
package fixedlist

import (
	"sync"
)

// 固定大小的队列
type SafeList[G any] struct {
	lock sync.RWMutex
	FixList[G]
}

// 创建新的固定大小队列
func NewSafeList[G any](size int) *SafeList[G] {
	m := &SafeList[G]{}
	m.SetSize(size)
	return m
}

// 创建新的固定大小队列
func NewSafeReserveList[G any](size, reserve int) *SafeList[G] {
	m := &SafeList[G]{}
	m.SetSize(size)
	m.reserve = reserve
	return m
}

// 重置队列
func (m *SafeList[G]) Reset() {
	m.lock.Lock()
	defer m.lock.Unlock()
	m.FixList.Reset()
}

// 设置队列大小
func (m *SafeList[G]) SetSize(maxLines int) {
	m.lock.Lock()
	defer m.lock.Unlock()
	m.FixList.SetSize(maxLines)
}

// 获取队列大小
func (m *SafeList[G]) Len() int {
	m.lock.RLock()
	defer m.lock.RUnlock()
	return m.FixList.Len()
}

// 检查队列是否为空
func (m *SafeList[G]) IsEmpty() bool {
	m.lock.RLock()
	defer m.lock.RUnlock()
	return m.FixList.IsEmpty()
}

// 检查队列是否已满
func (m *SafeList[G]) IsFull() bool {
	m.lock.RLock()
	defer m.lock.RUnlock()
	return m.FixList.IsFull()
}

// 入队操作
func (m *SafeList[G]) Push(e *G) {
	m.lock.Lock()
	defer m.lock.Unlock()
	m.FixList.Push(e)
}

// 获取指定位置的元素
func (m *SafeList[G]) Get(index int) *G {
	m.lock.RLock()
	defer m.lock.RUnlock()
	return m.FixList.Get(index)
}

// 倒序获取指定位置的元素
func (m *SafeList[G]) GetDesc(index int) *G {
	m.lock.RLock()
	defer m.lock.RUnlock()
	return m.FixList.GetDesc(index)
}

// 获取队首元素（不弹出）
func (m *SafeList[G]) Peek() (e *G) {
	m.lock.RLock()
	defer m.lock.RUnlock()
	return m.FixList.Peek()
}

// 出队操作
func (m *SafeList[G]) Pop() (e *G, err error) {
	m.lock.Lock()
	defer m.lock.Unlock()
	return m.FixList.Pop()
}

// 按队列顺序获取元素（先进先出）
func (m *SafeList[G]) Queue(begin, limit int, orderAsc ...bool) []*G {
	m.lock.RLock()
	defer m.lock.RUnlock()
	return m.FixList.Queue(begin, limit, orderAsc...)
}

// 按栈顺序获取元素（后进先出）
func (m *SafeList[G]) Stack(begin, limit int, orderAsc ...bool) []*G {
	m.lock.RLock()
	defer m.lock.RUnlock()
	return m.FixList.Stack(begin, limit, orderAsc...)
}

// Write .
func (m *SafeList[G]) Write(b []byte) (n int, err error) {
	m.lock.Lock()
	defer m.lock.Unlock()

	return m.FixList.Write(b)
}
